﻿
def calc_sum(*args):
	ax=0
	for n in args:
		ax=ax+n
	return ax

print "calc_sum =",calc_sum
print "calc_sum(1,2,3,4,5) =",calc_sum(1,2,3,4,5)

def lazy_sum(*args):
	def sum():
		ax=0
		for n in args:
			ax=ax+n
		return ax
	return sum

print "lazy_sum =",lazy_sum
ls=lazy_sum(1,2,3,4,5)
print "ls=lazy_sum(1,2,3,4,5) =",ls
print "ls() =",ls()

f1=lazy_sum(1,2,3,4,5)
f2=lazy_sum(1,2,3,4,5)
print "f1 = lazy_sum(1,2,3,4,5)"
print "f2 = lazy_sum(1,2,3,4,5)"
print "f1==f2 ",f1==f2

def count():
	fs=[]
	for i in range(1,4):
		def f():
			return i*i
		fs.append(f)
	return fs

f1,f2,f3=count()

print "f1,f2,f3=count()"

# 全部都是9！原因就在于返回的函数引用了变量i，但它并非立刻执行。等到3个函数都返回时，它们所引用的变量i已经变成了3，因此最终结果为9。
print "f1() =",f1()
print "f2() =",f2()
print "f3() =",f3()

def count2():
	fs=[]
	for i in range(1,4):
		def f(j):
			def g():
				return j*j
			return g
		fs.append(f(i))
	return fs

f1,f2,f3=count2()
print "f1,f2,f3=count2()"
print "f1() =",f1()
print "f2() =",f2()
print "f3() =",f3()
